import datetime
import pymysql
import PIL.ImageCms
from django.shortcuts import render
from django.db import models
from django.http import JsonResponse
# from django.shortcuts import render, render_to_response
from django.shortcuts import HttpResponseRedirect, HttpResponse, redirect
import random
from django.conf import settings
from register.models import Product_List, Product_Details, Image_Space, Product_Comment, Introduce_Myself, \
    Professional_skill
from .models import MyServices
from register import models
import json


# Create your views here.
# 我的能力的界面接口
def getMyServices(request):
    info = MyServices.objects.all()
    # print(info)
    list = []
    for item in info:
        obj = {
            'id': item.id,
            'Icon': item.Icon,
            'Number': item.Number,
            'Name': item.Name,
            'Introduction': item.Introduction,
        }
        list.append(obj)
    # print(list)
    jsondata = json.dumps(list)
    return HttpResponse(jsondata)


# 网站主页
def index(request):
    myself_info = Introduce_Myself.objects.filter(id=1)
    # print(myself_info)
    return render(request, "index.html", {'myself_info': myself_info})


# 产品展示页面
def work(request):
    page = request.GET.get('page')

    info = Product_List.objects.all().order_by('-id')
    if page:
        pageSize = int(request.GET.get('pageSize'))
        # 得到所有的图片信息
        page = int(page)
        # 进行分页
        page_all = (len(info) // pageSize)
        page_data = info[(page - 1) * pageSize:page * pageSize]
        page_previous = int(page - 1)
        if page_previous == 0:
            page_previous = 1
        page_next = int(page + 1)
        if page_next > page_all:
            page_next = page_all
        ll = []
        for i in page_data:
            obj = {
                "id": "{}".format(i.id),
                "Pname": "{}".format(i.Pname),
                "Pauthor": "{}".format(i.Pauthor),
                "Phref": "{}".format(i.Phref),
            }
            ll.append(obj)
        page_test = []
        page = {
            "page_first": "{}".format(1),
            "page": "{}".format(page),
            "page_all": "{}".format(page_all),
            "page_previous": "{}".format(page_previous),
            "page_next": "{}".format(page_next)
        }
        page_test.append(page)
        info_skill = Professional_skill.objects.all()
        info_list = []
        for item in info_skill:
            objss = {
                "Professional_Introduction": "{}".format(item.Professional_Introduction),
            }
            info_list.append(objss)
        list_list = []
        University_Major = Introduce_Myself.objects.all()
        for item in University_Major:
            obj = {
                "University": "{}".format(item.University),
                "Major": "{}".format(item.Major),
            }
            list_list.append(obj)
        return render(request, "work.html",
                      {"data": ll, "page": page_test, "objss": info_list, "University": list_list[0]['University'],
                       "Major": list_list[0]['Major']})
    else:
        page = 1
        # 进行分页
        page_all = (len(info) // 6) + 1
        page_data = info[(page - 1) * 6:page * 6]
        page_previous = int(page - 1)
        if page_previous == 0:
            page_previous = 1
        page_next = int(page + 1)
        ll = []
        for i in page_data:
            obj = {
                "id": "{}".format(i.id),
                "Pname": "{}".format(i.Pname),
                "Pauthor": "{}".format(i.Pauthor),
                "Phref": "{}".format(i.Phref),
            }
            ll.append(obj)
        page_test = []
        page = {
            "page_first": "{}".format(1),
            "page": "{}".format(page),
            "page_all": "{}".format(page_all),
            "page_previous": "{}".format(page_previous),
            "page_next": "{}".format(page_next),
        }
        page_test.append(page)
        info_skill = Professional_skill.objects.all()
        info_list = []
        for item in info_skill:
            objss = {
                "Introduce_Myself_ids_id": "{}".format(item.Introduce_Myself_ids_id),
                "Professional_Introduction": "{}".format(item.Professional_Introduction),
            }
            info_list.append(objss)
        list_list = []
        University_Major = Introduce_Myself.objects.all()
        for item in University_Major:
            obj = {
                "University": "{}".format(item.University),
                "Major": "{}".format(item.Major),
            }
            list_list.append(obj)
        return render(request, "work.html",
                      {"data": ll, "page": page_test, "objss": info_list, "University": list_list[0]['University'],
                       "Major": list_list[0]['Major']})


# 博客页面
def blog(request):
    mass = ''
    return render(request, "blog.html")


# 博客页面
def resume(request):
    mass = ''
    return render(request, "resume.html")


# 博客页面
def services(request):
    info = MyServices.objects.all()
    list = []
    for item in info:
        obj = {
            'id': item.id,
            'Icon': item.Icon,
            'Number': item.Number,
            'Name': item.Name,
            'Introduction': item.Introduction,
        }
        list.append(obj)
    # print(list)
    return render(request, "services.html", {'service': list})


# 博客页面
def contact(request):
    mass = ''
    return render(request, "contact.html")


# 点赞
def thumbsup(request, id):
    # print(id, type(id))
    if id == '0':
        return HttpResponseRedirect("/details_ids/{}".format(100))
    else:
        image_love = Image_Space.objects.get(id=id)
        image_love.Image_loves += 1
        image_love.save()
        iddd = image_love.Product_List_ids.id
        return HttpResponseRedirect("/details_ids/{}".format(iddd))


# 详情页
def details(request):
    return render(request, "details.html")


# 产品的详情
def details_ids(request, bid):
    l = []
    images_href = Image_Space.objects.filter(Product_List_ids_id=int(bid))
    if images_href:
        for i in images_href:
            obj1 = {
                "id": "{}".format(i.id),
                "Image_id": "{}".format(i.Image_id),
                "Image_name": "{}".format(i.Image_name),
                "Image_href": "{}".format(i.Image_href),
                "Image_loves": "{}".format(i.Image_loves),
                "Image_downloads": "{}".format(i.Image_downloads),
                "Image_comments": "{}".format(i.Image_comments)
            }
            l.append(obj1)
    else:
        obj2 = {
            "id": "0",
            "Image_id": "0",
            "Image_name": "test",
            "Image_href": "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2067513513,306548277&fm=26&gp=0.jpg",
            "Image_loves": "0",
            "Image_downloads": "0",
            "Image_comments": "0"
        }
        l.append(obj2)
    details_data = Product_Details.objects.filter(Product_List_id_id=int(bid))
    list_obj = []
    for item_detail in details_data:
        obj = {
            "id": "{}".format(item_detail.Product_List_id_id),
            "Product_name": "{}".format(item_detail.Product_name),
            "Item": "{}".format(item_detail.Item),
            "FOB_Price": "{}".format(item_detail.FOB_Price),
            "Port": "{}".format(item_detail.Port),
            "Minimum_Order_Quantity": "{}".format(item_detail.Minimum_Order_Quantity),
            "Supply_Ability": "{}".format(item_detail.Supply_Ability),
            "Payment_Terms": "{}".format(item_detail.Payment_Terms),
            "Brand_Name": "{}".format(item_detail.Brand_Name),
            "Product_Suitability": "{}".format(item_detail.Product_Suitability),
            "introduction": "{}".format(item_detail.introduction),
        }
        list_obj.append(obj)
    len_row = Product_Comment.objects.filter(Product_id_id=int(bid))
    return render(request, "details.html",
                  {"k2": l, "Image_comments": len(len_row), "id": list_obj[0]["id"],
                   "name": list_obj[0]["Product_name"],
                   "Brand_Name": list_obj[0]["Brand_Name"], "details": details_data})


# 提交按钮
def submits(request):
    Pname = request.POST.get('Pname')  # 项目名称
    Pauthor = request.POST.get('Pauthor')  # 作者
    datas = Product_List.objects.filter(Pname=Pname, Pauthor=Pauthor)
    for item in datas:
        id = item.id  # 查找唯一标识:id
    Commentator = request.POST.get('Commentator')  # 评论者
    Commentator_Tele = request.POST.get('Commentator_Tele')  # 评论者电话
    Commentator_Mailbox = request.POST.get('Commentator_Mailbox')  # 评论者邮箱
    Commentator_comment = request.POST.get('Commentator_comment')  # 评论者意见
    Commentator_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    models.Product_Comment.objects.create(Commentator=Commentator, Commentator_Tele=Commentator_Tele,
                                          Commentator_Mailbox=Commentator_Mailbox,
                                          Commentator_comment=Commentator_comment,
                                          Commentator_datetime=Commentator_datetime, Product_id_id=id)
    return render(request, "submits.html")


def test(request):
    datas = Product_List.objects.all()[2:4]
    return HttpResponse(datas)
